global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A37_fiveyear_difference.log, replace name(tabl)

capture noi { 
    * Table_fiveyear_difference.do
    global years 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009
    global window 5
    global shift 0

    qui do ${code_dir}/config/tabletools.do

    use ${final_dir}/regression_dataset_from1970_tfacit1_longdiff.dta, clear 

    * Panel A is firms which patented at least once; panel B is firms which patented at least twice; Panel C is using ISP
    local panels A B C
    foreach panel of local panels { 
        * set configurations
        if "`panel'" == "A" {
            local total 1
            local indep "LSW HSW"
            local indep_foreign "LSW_foreign HSW_foreign"
        }
        if "`panel'" == "B" {
            local total 2
            local indep "LSW HSW"
            local indep_foreign "LSW_foreign HSW_foreign"
        }
        if "`panel'" == "C" {
            local total 1
            local indep "ISP"
            local indep_foreign "ISP_foreign"
        }
        preserve
        * Keep firms which patented at least once/twice in 1995-2013
        bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995 & year <= 2013
        bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
        drop _total_${depvar}_${ttt}_1995
        keep if year>=1995 & missing_weights_1995 == 0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995 >= `total'
        mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
        keep if _m == 3
        drop total
        keep if inlist(year,${years})

        clonevar LSW = D${window}lswMPm_1995_a
        clonevar HSW = D${window}hswMPm_1995_a
        clonevar ISP = D${window}ispMPm_1995_a
        clonevar VAEMP = D${window}vaempMPm_1995_a
        clonevar GDPGAP = D${window}lngdpgap_1995_a
        clonevar GDPPC = D${window}gdppcMPm_1995_a
        clonevar spilloversown = D${window}spill${depvar}${ttt}_1995_a
        clonevar spilloversother = D${window}spillN${depvar}${ttt}_1995_a
        
        cap drop pats*

        egen yrindustry = group(year industry) if year <= 2009
        egen yrctry = group(year country_shr_1995)  if year <= 2009

        * No CY FE(cols 1,2,7)
        reghdfe diff_F${window}L${window}_${depvar}_${ttt} `indep' GDPGAP VAEMP spilloversown spilloversother, vce(cluster lse_id)  absorb( year yrindustry)
        estadd local iy "\yes"
        estadd local cy "\no"
        hasvar  spill*, local(spill)
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store `panel'1
        
        * CY FE (cols 3,4,8)
        reghdfe diff_F${window}L${window}_${depvar}_${ttt} `indep' GDPGAP VAEMP  spilloversown spilloversother, vce(cluster lse_id)  absorb(year  yrindustry  yrctry)
        estadd local iy "\yes"
        estadd local cy "\yes"
        hasvar  spill*, local(spill)
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store `panel'2

        drop `indep' GDPGAP GDPPC VAEMP

        clonevar GDPPC_foreign = D${window}gdppcMPm_shr4_foreign_1995_a
        clonevar VAEMP_foreign = D${window}vaempMPm_shr4_foreign_1995_a
        clonevar GDPgap_foreign = D${window}lngdpgap_shr_foreign_1995_a
        clonevar LSW_foreign = D${window}lswMPm_shr4_foreign_1995_a
        clonevar HSW_foreign = D${window}hswMPm_shr4_foreign_1995_a
        clonevar ISP_foreign = D${window}ispMPm_shr4_foreign_1995_a
        
        * CY FE and normalized foreign variables (cols 5,6,9)
        reghdfe diff_F${window}L${window}_${depvar}_${ttt} `indep_foreign' GDPgap_foreign VAEMP_foreign spilloversown spilloversother, vce(cluster lse_id)  absorb(year  yrindustry  yrctry)
        estadd local iy "\yes"
        estadd local cy "\yes"
        hasvar  spill*, local(spill)
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store `panel'3
        restore
    }

    * Output table
    setlabels

    dhoztab A1 C1 A2 C2 A3 C3 B1 B2 B3 using ${tab_dir}/appendix/Table_A37_fiveyear_difference.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_spill \noalign{\medskip}  Spillovers" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") /// 
        rawaddheader({Firm restriction}&\multicolumn{6}{c}{At least one auto95 innovation}&\multicolumn{3}{c}{At least two auto95 innovations}\\ \cmidrule(lr){2-7}\cmidrule(lr){8-10}) mgroups("Domestic and Foreign" "Foreign" "Dom. and Fgn." "Fgn." "Dom. and Fgn." "Fgn.", pattern(1 0 0 0 1 0 1 0 1) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
        drop(_cons spill*) noobs label rename(LSW D_LSW HSW D_HSW GDPGAP D_GDPGAP VAEMP D_VAEMP GDPPC D_GDPPC ISP D_ISP LSW_foreign D_LSW HSW_foreign D_HSW GDPgap_foreign D_GDPGAP VAEMP_foreign D_VAEMP GDPPC_foreign D_GDPPC ISP_foreign D_ISP) order(D_LSW D_HSW D_ISP D_GDPGAP D_VAEMP)  ///
        depvar("$\Delta$ Arcsinhauto95") ///
        notes("This table conducts five-year difference regressions. Estimation is done by OLS for the years t=2000-2009. The dependent variable is the difference between the arcsinh of the sum of yearly auto95 patents in t to t+4 and the arcsinh of the sum of yearly auto95 patents in t-5 to t-1. All independent variables are the sum of yearly counterparts from t-4 to t. Columns 1--6 focus on firms that have at least patented once in 1995--2013 while columns 7--9 restrict attention to firms that patented at least twice in 1995--2013. All columns include industry-year fixed effects. Columns 3--6 and 8--9 add country-year fixed effects. In Columns 5, 6, and 9 the macroeconomic variables are normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.")
    
	*again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}
cap log close tabl